package com.finance.cooperate.strategy.service.impl;

import com.finance.cooperate.common.constant.CodeMsg;
import com.finance.cooperate.common.pojo.RestResult;
import com.finance.cooperate.common.utils.StringUtil;
import com.finance.cooperate.dao.entity.secondary.StrategyInfo;
import com.finance.cooperate.dao.repository.secondary.StrategyInfoRepository;
import com.finance.cooperate.dao.support.constant.DeleteStatus;
import com.finance.cooperate.dao.support.query.Filter;
import com.finance.cooperate.dao.support.query.QueryParams;
import com.finance.cooperate.strategy.common.dto.StrategyInfoAndWeightDTO;
import com.finance.cooperate.strategy.common.params.StrategyParams;
import com.finance.cooperate.strategy.core.StrategyProcessorHolder;
import com.finance.cooperate.strategy.service.StrategyInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @ClassName StrategyInfoServiceImpl
 * @Description TODO
 * @Author shenanxin
 * @Date 2023/8/23 15:52
 * @Modify ...
 */
@Service
@Slf4j
public class StrategyInfoServiceImpl implements StrategyInfoService {

    @Autowired
    private StrategyInfoRepository strategyInfoRepository;

    @Autowired
    private StrategyProcessorHolder processorHolder;


    @Override
    public StrategyInfoAndWeightDTO findAllStrategyInfoAndWeightByGroup(String group) {


        QueryParams<StrategyInfo> queryParams = new QueryParams<>();
        queryParams.and(
                Filter.eq("status", 1),
                Filter.eq("isDeleted", DeleteStatus.NO),
                Filter.eq("group", group),
                Filter.gt("weight", 0)
        );

        List<StrategyInfo> strategyInfoList = strategyInfoRepository.findAll(queryParams);


        StrategyInfoAndWeightDTO dto = new StrategyInfoAndWeightDTO();

        double[] weightList = new double[strategyInfoList.size()];

        for (int i = 0; i < strategyInfoList.size(); i++) {
            weightList[i] = strategyInfoList.get(i).getWeight();
        }

        dto.setWeightList(weightList);
        dto.setStrategyInfos(strategyInfoList);

        return dto;
    }



    @Override
    public RestResult strategy(StrategyParams params) {

        RestResult result = RestResult.build(CodeMsg.FAILURE);

        // 参数校验
        if (params == null) {

            return RestResult.build(CodeMsg.PARAMS_NULL);

        } else if (StringUtil.isEmptyOrNvl(params.getUid())) {

            return RestResult.build(CodeMsg.USER_ID_PARAMS_NULL);

        } else if (StringUtil.isEmptyOrNvl(params.getScore())) {

            return RestResult.build(CodeMsg.SCORE_PARAMS_NULL);

        } else if (StringUtil.isEmptyOrNvl(params.getLoanNum())) {

            // 默认首贷
            params.setLoanNum(1L);

        }


        try {

            return processorHolder.run(params);

        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage(), e);
        }

        return result;
    }
}
